<div class="layui-card">
    <div class="layui-card-header">
        <h2 class="header-title">字典管理</h2>
        <span class="layui-breadcrumb pull-right">
          <a href="#!console">首页</a>
          <a><cite>字典管理</cite></a>
        </span>
    </div>
    <div class="layui-card-body">
        <div class="layui-form toolbar">
            搜索：
            <select id="dictionary-search-key">
                <option value="">-请选择-</option>
                <option value="dictid">ID</option>
                <option value="dccode">字典代码</option>
                <option value="dcname">字典名</option>
                <option value="dctype">字典类型</option>
            </select>&emsp;
            <input id="dictionary-search-value" class="layui-input search-input" type="text" placeholder="输入称号类型名称"/>&emsp;
            <button id="dictionary-btn-search" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
            <button id="dictionary-btn-add" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加字典类型</button>
        </div>

        <table class="layui-table" id="dictionary-table" lay-filter="dictionary-table"></table>
    </div>
</div>


<!-- 表格操作列 -->
<script type="text/html" id="dictionary-table-bar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
<!--    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>-->
</script>

<!-- 表格状态列 -->
<script type="text/html" id="dictionary-tpl-state">
    <input type="checkbox" lay-filter="dictionary-tpl-state" value="{{d.dictid}}" lay-skin="switch" lay-text="正常|锁定"
           {{d.enabled==true?'checked':''}}/>
</script>

<script>
    layui.use(['form', 'index','table', 'util', 'config', 'admin'], function () {
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var layer = layui.layer;
        var util = layui.util;
        var admin = layui.admin;

        // 渲染表格
        table.render({
            elem: '#dictionary-table',
            // url: config.base_server + 'user_query.json',
            url: config.base_server + 'api-user/dictionary/getSelectDictionaries'+'?access_token='+config.getToken().access_token,
            method: 'POST',
            where: {
                // access_token: config.getToken().access_token
            },
            page: true,
            cols: [[
                {type: 'numbers'},
                {field: 'dictid', sort: true, title: 'ID'},
                {field: 'dccode', sort: true, title: '字典代码'},
                {field: 'dcname', sort: true, title: '字典名'},
                {field: 'dctype',sort : true, title :'字典类型'},
                {
                    sort: true, templet: function (d) {
                        return util.toDateString(d.cretim);
                    }, title: '创建时间'
                },
                {field: 'enabled', sort: true, templet: '#dictionary-tpl-state', title: '状态',width: 150},
                {align: 'center', toolbar: '#dictionary-table-bar', title: '操作',width: 150}
            ]]
        });
        // console.log(filed);

        // 工具条点击事件
        table.on('tool(dictionary-table)', function (obj) {
            var data = obj.data;
            obj.event === 'edit' //修改
                showEditModel(data);
            // if (obj.event === 'edit') { //修改
            //     showEditModel(data);
            // } else if (obj.event === 'del') { //删除
            //     doDelete(obj);
            // }
        });


        // 搜索按钮点击事件
        $('#dictionary-btn-search').click(function () {
            var key = $('#dictionary-search-key').val();
            var value = $('#dictionary-search-value').val();
            table.reload('dictionary-table', {where: {searchKey: key, searchValue: value}});
        });

        // 添加按钮点击事件
        $('#dictionary-btn-add').click(function () {
            showEditModel();
        });


        //显示修改表单弹窗
        var showEditModel = function (data) {
            admin.putTempData('dictionary', data);
            var title = data ? '修改字典类型':'添加字典类型';
            admin.popupCenter({
                title: title,
                path: 'pages/system/dictionary_form.html',
                finish: function () {
                    table.reload('dictionary-table', {});
                }
            });
        };

        // 修改dictionary状态
        form.on('switch(dictionary-tpl-state)', function (obj) {
            layer.load(2);
            admin.req('api-user/dictionary/updateDictionaryEnabled', {
                id: obj.elem.value,
                enabled: obj.elem.checked ? true : false
            }, function (data) {
                layer.closeAll('loading');
                if (data.code == 0) {
                    layer.msg(data.msg, {icon: 1, time: 500});
                    //table.reload('table-user', {});
                } else {
                    layer.msg(data.msg, {icon: 2, time: 500});
                    $(obj.elem).prop('checked', !obj.elem.checked);
                    form.render('checkbox');
                }
            }, 'GET');
        });
    });
</script>
